package com.jsh.erp.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.jsh.erp.constants.BusinessConstants;
import com.jsh.erp.datasource.entities.Contract;
import com.jsh.erp.datasource.entities.Dict;
import com.jsh.erp.datasource.entities.DictExample;
import com.jsh.erp.datasource.entities.Supplier;
import com.jsh.erp.datasource.mappers.DictMapper;
import com.jsh.erp.service.contract.ContractComponent;
import com.jsh.erp.service.contract.ContractService;
import com.jsh.erp.service.project.ProjectService;
import com.jsh.erp.service.systemConfig.SystemConfigService;
import com.jsh.erp.service.user.UserService;
import com.jsh.erp.service.userBusiness.UserBusinessService;
import com.jsh.erp.utils.Constants;
import com.jsh.erp.utils.ErpInfo;
import com.jsh.erp.utils.ParamUtils;
import com.jsh.erp.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jsh.erp.utils.ResponseJsonUtil.returnJson;

/**
 * @author ji|sheng|hua 华夏erp
 */
@RestController
@RequestMapping(value = "/dict")
@Api(tags = {"字典管理"})
public class DictController {
    private Logger logger = LoggerFactory.getLogger(DictController.class);

    @Resource
    private DictMapper dictMapper;

    @GetMapping(value = "/findBySelect")
    @ApiOperation(value = "根据字典类型查询")
    public JSONArray findBySelect(HttpServletRequest request)throws Exception {
        Map<String, String> parameterMap = ParamUtils.requestToMap(request);

        DictExample dictExample = new DictExample();
        dictExample.createCriteria()
                .andDeleteFlagEqualTo("0")
                .andTypeEqualTo(parameterMap.get("type"))
                .andLabelLike("%"+parameterMap.get("label")+"%");
        List<Dict> dictList = dictMapper.selectByExample(dictExample);

        JSONArray dataArray = new JSONArray();
        if (null != dictList) {
            for (Dict dict : dictList) {
                JSONObject item = new JSONObject();
                item.put("value", dict.getValue());
                item.put("label", dict.getLabel());
                dataArray.add(item);
            }
        }
        return dataArray;
    }



}
